﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace S.Sams.GoogleMap.Web
{
    /// <summary>
    /// MapPageBase 处理了 // onload="initialize()" onunload="GUnload()" 这两个事件, 如果不使用继承, 请自行处理该事件!
    /// </summary>
    public partial class Default : MapPageBase
    {
        #region 数据初始化
        private string AppID
        {
            get { return appid.Text; }
        }

        private Init GetSystemInit
        {
            get
            {
                Init init = new Init(showmymap2.Text, int.Parse(scaleint.Text), MapType.Normal, MapControl.Full);
                init.EnabledScrollWheelZoom = EnabledScrollWheelZoom.Checked;
                init.IsMapScale = mapscale.Checked;
                init.IsMapView = mapview.Checked;
                init.IsMapType = IsMapType.Checked;
                init.IsMapControls = IsMapControls.Checked;
                init.IsShowCenterMsg = IsShowCenterMsg.Checked;
                init.MapViewWidth = int.Parse(MapViewWidth.Text);
                init.MapViewHeight = int.Parse(MapViewHeight.Text);
                init.IsAddLine = IsAddLine.Checked;
                init.IsAddLineEnd = IsAddLineEnd.Checked;
                init.AddLine16Color = AddLine16Color.Text;
                init.AddLineWidth = int.Parse(AddLineWidth.Text);
                init.Customlabel = new CustomLabel() { 
                    isEnabledCusstomLabel = isEnabledUserCusstom.Checked, 
                    isOnlyOnePoint = isUsePointOne.Checked, 
                    PointMsg = UsePointOneMsg.Text, 
                    JavascriptFunction = FunctionName.Text, 
                    isClickShowMsg = isClickShowMsg.Checked 
                };
                init.EnableGoogleBar = EnableGoogleBar.Checked;
                init.EnableGoogleBarShow = EnableGoogleBarShow.Checked;
                return init;
            }
        }

        private List<Label> GetLabels
        {
            get
            {
                return new List<Label>()
                {
                    new Label(){ 
                        TabMsg = new List<Msg>() {
                                    new Msg("环境", L1_msg.Text),
                                    new Msg("饮食", "仅次于一般"),
                                    new Msg("一起飞", "<div><strong>一起飞国际机票网</strong></div><img src='http://jp.yiqifei.com/images/logo.png' />")
                                },
                        IsCenter = L1_iscenter.Checked,
                        LabelImage = L1_img.Text,
                        X = double.Parse(L1_x.Text),
                        Y = double.Parse(L1_y.Text),
                        ZoomIn = int.Parse(L1_zoomin.Text),
                        IsShowMapView = L1_mapview.Checked,
                        Visible = L1_visible.Checked,
                        MsgMaxTitle = L1_maxtitle.Text,
                        MsgMaxContent = L1_maxcontent.Text
                    },
                    new Label(){ 
                        Msg=L2_msg.Text,
                        IsCenter = L2_iscenter.Checked,
                        LabelImage = L2_img.Text,
                        X = double.Parse(L2_x.Text),
                        Y = double.Parse(L2_y.Text),
                        ZoomIn = int.Parse(L2_zoomin.Text),
                        IsShowMapView = L2_mapview.Checked,
                        Visible = L2_visible.Checked,
                        MsgMaxTitle = L2_maxtitle.Text,
                        MsgMaxContent = L2_maxcontent.Text
                    },
                    new Label(){ 
                        Msg=L3_msg.Text,
                        IsCenter = L3_iscenter.Checked,
                        LabelImage = L3_img.Text,
                        X = double.Parse(L3_x.Text),
                        Y = double.Parse(L3_y.Text),
                        ZoomIn = int.Parse(L3_zoomin.Text),
                        IsShowMapView = L3_mapview.Checked,
                        Visible = L3_visible.Checked,
                        MsgMaxTitle = L3_maxtitle.Text,
                        MsgMaxContent = L3_maxcontent.Text
                    },
                    new Label(){ 
                        Msg=L4_msg.Text,
                        IsCenter = L4_iscenter.Checked,
                        LabelImage = L4_img.Text,
                        X = double.Parse(L4_x.Text),
                        Y = double.Parse(L4_y.Text),
                        ZoomIn = int.Parse(L4_zoomin.Text),
                        IsShowMapView = L4_mapview.Checked,
                        Visible = L4_visible.Checked,
                        MsgMaxTitle = L4_maxtitle.Text,
                        MsgMaxContent = L4_maxcontent.Text
                    },
                    new Label(){ 
                        Msg=L5_msg.Text,
                        IsCenter = L5_iscenter.Checked,
                        LabelImage = L5_img.Text,
                        X = double.Parse(L5_x.Text),
                        Y = double.Parse(L5_y.Text),
                        ZoomIn = int.Parse(L5_zoomin.Text),
                        IsShowMapView = L5_mapview.Checked,
                        Visible = L5_visible.Checked,
                        MsgMaxTitle = L5_maxtitle.Text,
                        MsgMaxContent = L5_maxcontent.Text
                    }
                };
            }
        }

        private List<MapLine> GetMapLines
        {
            get {
                return new List<MapLine>(){new MapLine() {
                    Labels = GetLabels,
                    LineWidth = int.Parse(GetMapLines_Width.Text),
                    Color16Code = GetMapLines_Color.Text}
                };
            }
        }

        #endregion

        private GoogleMap.Maps map;

        private void BuiderBtns()
        {
            List<JavascriptFunction> getcb = map.CallFunction();
            BuiderBtn.Text = string.Empty;
            foreach (JavascriptFunction jf in getcb)
            {
                BuiderBtn.Text += string.Format("<button onclick=\"{0};\">{0}</button>", jf.Function);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            map = new Maps(AppID, IsUseLocation.Checked, IsPackJs.Checked, GetSystemInit, GetLabels, GetMapLines);
            map.GoogleMapsJsPath = _googlemapsjs.Text.Trim();

            if (!this.Page.IsPostBack)
            {
                if (!addLines.Checked) map.Maplines = null;
                GooleMap.Text = map.ToString();
                BuiderBtns();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!addLines.Checked) map.Maplines = null;
            GooleMap.Text = map.ToString();
            BuiderBtns();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Button1_Click(new object(), EventArgs.Empty);
            _imagesrc.Text = _imgtemp.Text.Replace("{GoogleMapsImg}",map.ToImagePath(int.Parse(_width.Text.Trim()), int.Parse(_height.Text.Trim())));
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            map.SystemInit.Maptype = MapType.G_PHYSICAL_MAP;
            map.SystemInit.ZoomIn = 5;
            Button1_Click(new object(), EventArgs.Empty);
        }
    }
}
